package com.alumni_circle.core.junit;

import net.minidev.json.JSONObject;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import java.util.Map;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * @author 龙小虬
 * @since 2020-07-08 21:10
 */

@RunWith(SpringRunner.class)
@SpringBootTest
public abstract class BaseJunit {

    /**
     * 模拟MVC对象，通过MockMvcBuilders.webAppContextSetup(this.wac).build()初始化。
     */
    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext wac;

    @Before
    public void setup() {
        // 在测试开始前初始化工作
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    public void getTest(String path) throws Exception{
        mockMvc.perform(MockMvcRequestBuilders.get("/"+path))
                .andExpect(status().isOk())
                .andDo(MockMvcResultHandlers.print());
    }

    public void postTest(String path,Map<String,Object> map) throws Exception{
        MvcResult result = mockMvc.perform(post("/"+path)
                .contentType(MediaType.APPLICATION_JSON).content(JSONObject.toJSONString(map)))
                // 模拟向testRest发送post请求
                .andExpect(status().isOk())
                // 预期返回值的媒体类型application/json;charset=UTF-8
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
                // 返回执行请求的结果
                .andReturn();
        System.out.println(result);
    }

    public void getSecurityTest(String path) throws Exception{
        mockMvc.perform(MockMvcRequestBuilders.get("/"+path))
                .andExpect(status().isOk())
                .andDo(MockMvcResultHandlers.print());
    }

    public void postSecurityTest(String path,Map<String,Object> loginMap,Map<String,Object> map) throws Exception{

        MvcResult token = mockMvc.perform(post("/"+path)
                .contentType(MediaType.APPLICATION_JSON).content(JSONObject.toJSONString(loginMap)))
                // 模拟发送post请求
                .andExpect(status().isOk())
                // 预期返回值的类型application/json;charset=UTF-8
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
                // 返回执行请求的结果
                .andReturn();

        System.out.println(token);


        MvcResult result = mockMvc.perform(post("/"+path)
                .contentType(MediaType.APPLICATION_JSON).content(JSONObject.toJSONString(map)))
                // 模拟发送post请求
                .andExpect(status().isOk())
                // 预期返回值的媒体类型application/json;charset=UTF-8
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
                // 返回执行请求的结果
                .andReturn();
        System.out.println(result);
    }

}
